﻿using DocumentFormat.OpenXml.Drawing.Spreadsheet;
using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：OPC
    /// </summary>
    public class ec_OPCBLL
    {
        ec_OPCService Service;
        public ec_OPCBLL()
        {
            Service = new ec_OPCService();
        }
        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public List<ec_OPCEntity> GetList(string projId)
        {
            try
            {
                var list = Service.GetList(projId);
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 获取列表数据，并转为<see cref="ec_enginedataEntity"/>
        /// <summary>
        /// <returns></returns>
        public List<ec_enginedataEntity> GetList2EngineData(string projId, string drawingFileID, string pixelCodes)
        {
            try
            {
                var res = new List<ec_enginedataEntity>();
                var list = Service.GetList(projId); 
                if (!string.IsNullOrEmpty(drawingFileID) && !string.IsNullOrEmpty(pixelCodes))
                {
                    var pixelTbName = ProjectSugar.TableName<ec_enginedata_pixelEntity>(projId);
                    var opcids = Db.Queryable<ec_enginedata_pixelEntity>().AS(pixelTbName).
                        Where(x => x.DrawingFileID == drawingFileID && pixelCodes.Contains(x.PixelCode)).
                        Select(x=>x.EngineDataID).
                        ToList();

                    list = list.Where(x => opcids.Contains(x.OPCID)).ToList();//找出符合图元记录的opc
                }

                foreach (var item in list)
                {
                    res.Add(new ec_enginedataEntity()
                    {
                        ObjectTypeID = "OPC",
                        TagNumber = item.OPCTAG,
                        ObjectTypeName = "OPC",
                        EngineDataID = item.OPCID
                    });
                }
                return res;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        public List<ec_objecttypepEntity> GetOPCPropertyDef()
        {
            var res = new List<ec_objecttypepEntity>();
            var fakeProp = new ec_objecttypepEntity()
            {
                ObjectTypePID = "OPCP_Service",
                ObjectTypeID = "OPC",
                PropertyID = "OPC_Service",
                PropertyGID = "OPC",
                PropertyGroupName = "OPC",
                PropertyName = "用途",
                PropertyNameEN = "Service",
                PropertyType = "00001",
                EnumData = "",
                EnumDataCode = null,
                MeasuringUnitTypeName = null,
                DefaultUnitName = null,
                MeasuringUnitTypeID = "",
                DefaultUnit = "",
                DefaultValue = ""

            };
            res.Add(fakeProp);
            fakeProp = new ec_objecttypepEntity()
            {
                ObjectTypePID = "OPCP_FromText",
                ObjectTypeID = "OPC",
                PropertyID = "OPC_FromText",
                PropertyGID = "OPC",
                PropertyGroupName = "OPC",
                PropertyName = "From信息",
                PropertyNameEN = "From Text",
                PropertyType = "00001",
                EnumData = "",
                EnumDataCode = null,
                MeasuringUnitTypeName = null,
                DefaultUnitName = null,
                MeasuringUnitTypeID = "",
                DefaultUnit = "",
                DefaultValue = ""

            };
            res.Add(fakeProp);
            fakeProp = new ec_objecttypepEntity()
            {
                ObjectTypePID = "OPCP_ToText",
                ObjectTypeID = "OPC",
                PropertyID = "OPC_ToText",
                PropertyGID = "OPC",
                PropertyGroupName = "OPC",
                PropertyName = "To信息",
                PropertyNameEN = "To Text",
                PropertyType = "00001",
                EnumData = "",
                EnumDataCode = null,
                MeasuringUnitTypeName = null,
                DefaultUnitName = null,
                MeasuringUnitTypeID = "",
                DefaultUnit = "",
                DefaultValue = ""

            };
            res.Add(fakeProp);
            return res;
        }

        public List<ec_enginedata_propertyEntity> GetProps2EngineProp(string projId, string OPCId = "")
        {
            var resAll = new List<ec_enginedata_propertyEntity>();
            var list = Service.GetList(projId);
            if (!string.IsNullOrEmpty(OPCId))
            {
                list = list.Where(x => x.OPCID == OPCId).ToList();
            }
            foreach (var opc in list)
            {
                var prop = new ec_enginedata_propertyEntity()
                {
                    EngineDataPropertyID = "",
                    EngineDataID = opc.OPCID,
                    PropertyName = "用途",
                    PropertyNameEN = "Service",
                    PropertyValue = opc.DESC,
                    CaseID = "0",
                    PropertyGID = "OPC",
                     PropertyGroupName = "OPC",
                      PropertyID = "OPC_Service"

                };
                resAll.Add(prop);
                prop = new ec_enginedata_propertyEntity()
                {
                    EngineDataPropertyID = "",
                    EngineDataID = opc.OPCID,
                    PropertyName = "From信息",
                    PropertyNameEN = "FromText",
                    PropertyValue = opc.FROMTEXT,
                    CaseID = "0",
                    PropertyGID = "OPC",
                    PropertyGroupName = "OPC",
                    PropertyID = "OPC_FromText"

                };
                resAll.Add(prop);
                prop = new ec_enginedata_propertyEntity()
                {
                    EngineDataPropertyID = "",
                    EngineDataID = opc.OPCID,
                    PropertyName = "To信息",
                    PropertyNameEN = "ToText",
                    PropertyValue = opc.TOTEXT,
                    CaseID = "0",
                    PropertyGID = "OPC",
                    PropertyGroupName = "OPC",
                    PropertyID = "OPC_ToText"

                };
                resAll.Add(prop);
            }


            return resAll;
        }
        #endregion


    }
}
